//
//  AppDelegate.swift
//  HMWeibo
//
//  Created by wulei on 15/12/6.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        setupApparence()
//        print(HMUserAccountViewModel.sharedUserAccountViewModel.account)
window = UIWindow(frame: UIScreen .mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        // HMNewFeatrueCollectionViewController()
        // MainViewController()
        // HMWelcomeViewController()
        window?.rootViewController = defaultRootViewController
        window?.makeKeyAndVisible()
        
        
        // 监听通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification,// 通知名称,通知中心用来识别通知的
            object: nil,                            // 发送通知的对象,如果为nil,监听任何对象
            queue: nil)                             // nil 为主线程
            { [weak self] (notification) -> Void in
                print(NSThread .currentThread())
//            print(notification)
                
               let VC = notification.object != nil ? HMWelcomeViewController() : MainViewController()
                
                self!.window?.rootViewController = VC
        }
        
        SQLiteManager.sharedManager
        return true
    }

    // 注销通知 - 注销指定的通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self, // 监听者
            name: WBSwitchRootViewControllerNotification,         // 监听的通知
            object: nil)                                          // 发送通知的对象
    }
    
    // 设置全局外观
    private func setupApparence() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    }

    
    
}


// MARK: - 界面切换
extension AppDelegate {
    
    // 判断要启动的根控制器
    // 计算型属性
   private var defaultRootViewController: UIViewController {
        if HMUserAccountViewModel.sharedUserAccountViewModel.isLogon{
        
            // 1.如果有新版本,跳转新特性,否则是欢迎界面
        return isNewVersion ? HMNewFeatrueCollectionViewController(): HMWelcomeViewController()
        }
        
        return MainViewController()
}
    
    private var isNewVersion: Bool {
        
        // 1.当前的版本 info.plist
//        print(NSBundle .mainBundle().infoDictionary)
        let currenVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as? String
        let version = Double(currenVersion!)
//        print("当前版本\(version)")
        
        
        // 2.之前的版本 保存在偏好设置,如果不存在,返回值为 0
        let sandBoxVersionKey = "sandBoxVersionKey"
        let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBoxVersionKey)
//        print("之前的版本\(sandBoxVersion)")
        
        // 3.保存当前的版本到偏好设置
        NSUserDefaults.standardUserDefaults().setDouble(version!, forKey: sandBoxVersionKey)
        
        return version > sandBoxVersion
    }
    
}
